<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-tabs v-model="activeName">
          <el-tab-pane label="角色管理" name="first">
            <el-button type="primary" size="small" icon="el-icon-plus" @click="addRole">角色管理</el-button>
            <el-table :data="list" style="width: 100%">
              <el-table-column type="index" label="序号" align="center" />
              <el-table-column prop="name" label="角色名称" align="center" />
              <el-table-column prop="description" label="描述" align="center" />
              <el-table-column label="操作" align="center">
                <!-- // 作用域插槽 -->
                <template #default="scoped">
                  <el-button size="small" type="success">分配权限</el-button>
                  <el-button size="small" type="primary" @click.native="edit(scoped.row)">编辑</el-button>
                  <el-button size="small" type="danger" @click="del(scoped.row)">删除</el-button>
                </template>
              </el-table-column>
            </el-table>
          </el-tab-pane>
          <el-tab-pane label="公司信息" name="second">公司信息</el-tab-pane>
        </el-tabs>
        <el-row
          type="flex"
          justify="center"
          align="middle"
          style="height: 60px"
        >
          <el-pagination
            :page-size="pageOption.pagesize"
            :total="total"
            :current-page="pageOption.page"
            :hide-on-single-page="true"
            layout="prev, pager, next"
            @current-change="currentChangeFn"
          />
        </el-row>
      </el-card>
    </div>
    <!-- 添加角色弹出层 -->
    <el-dialog
      :title="title"
      :visible="dialogShow"
      width="30%"
      @close="closeDialog"
    >
      <el-form ref="ruleForm" :model="dataForm" status-icon :rules="formRules" label-width="100px">
        <el-form-item label="角色名称" prop="name">
          <el-input v-model="dataForm.name" />
        </el-form-item>
        <el-form-item label="角色描述" prop="description">
          <el-input v-model="dataForm.description" />
        </el-form-item>

      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="closeDialog">取 消</el-button>
        <el-button type="primary" @click="submit">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { getRoleList, addRole, getRoleDetail, upDataRole, delRole } from '@/api'
export default {
  data() {
    return {
      dialogShow: false, // 弹出添加角色框
      dataForm: {
        name: '',
        description: ''
      },
      formRules: {
        name: [{ required: true, message: '角色名称不能为空', trigger: 'blur' }]
        // description: []
      },
      pageOption: {
        page: 1,
        pagesize: 10
      },
      activeName: 'first',
      list: [],
      total: 0,
      currentNode: {} // 节点 item，

    }
  },
  computed: {
    title() {
      return this.dataForm.id ? '编辑角色' : '新增角色'
    }
  },
  async created() {
    this.fecthRoleList()
  },
  methods: {
    // 删除
    del(val) {
      this.$confirm('确定删除该条数据？', '提示', {
        cancelButtonText: '取消',
        confirmButtonText: '确定',
        type: 'warning'
      }).then(async() => {
        await delRole(val.id)
        this.$message.success('删除成功')
        // 如果页面只剩最后一个条信息 并且 不是第一页  让页面减一 从新渲染 请求
        if (this.list.length === 1 && this.pageOption.page !== 1) {
          this.pageOption.page--
        }
        this.fecthRoleList()
      }).catch(() => {
        this.$message.success('取消删除')
      })// 在这里可以调接口，注意：.then后面要写一个回调函数
    },
    // 编辑
    async edit(val) {
      this.currentNode = val // 存一份 node节点item
      const { data } = await getRoleDetail(val.id)
      console.log(data)
      this.dataForm = data // 数据回显
      this.dialogShow = true
    },
    // 新增角色
    addRole() {
      this.dialogShow = true
    },
    // 关闭弹窗触发函数 退出 清空表单
    closeDialog() {
      // 重置表单 清空表单
      this.$refs.ruleForm.resetFields()
      this.dataForm = {
        name: '',
        description: ''
      }
      this.dialogShow = false
    },
    submit() {
      this.$refs.ruleForm.validate(async(valid) => {
        if (!valid) return
        if (this.dataForm.id) { // 判断是否是新增还是 编辑
          await upDataRole(this.dataForm)
        } else {
          await addRole(this.dataForm)
        }

        this.$message.success(this.title + '成功')
        // 清空表单
        this.closeDialog()
        // 拿去最新数据
        this.fecthRoleList()
      })
    },
    // 获取
    async fecthRoleList() {
      const { data } = await getRoleList(this.pageOption)
      console.log(data)
      this.list = data.rows
      this.total = data.total
    },
    // 页码
    currentChangeFn(val) {
      console.log(val) // 当前页
      this.pageOption.page = val
      this.fecthRoleList()
    }
  }
}
</script>

